package com.dehui.traders.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.ui.stockdetail.adapter.StocksDetailSyAdapter;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.25.
 */

public class DetailTableView extends LinearLayout implements TableAdapter.OnTableData{

    protected List<MyHScrollView> mHScrollViews = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private int screenWidth;
    private View headView;
    private View lastSelectView;
    private String[] mFixes;
    private String[] mTotals;
    private String[] mOthers;
    private Fragment mFragment;
    private Activity mActivity;
    private StocksDetailSyAdapter mAdapter;
    private TableAdapter.OnTableItemClick onTableItemClick;

    public DetailTableView(Context context) {
        super(context,null);
    }

    public DetailTableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    private void init() {
        this.setOrientation(LinearLayout.VERTICAL);
        inflater = LayoutInflater.from(context);
        screenWidth = Utils.getScreen(context).widthPixels;
        removeAllViews();
        intiTabHead();
        initTabContent();
    }

    private void intiTabHead(){
        headView = inflater.inflate(R.layout.tab_head_detail_top, null);
        TextView tvSy0 = headView.findViewById(R.id.tv_sy0);
        TextView tvSy1 = headView.findViewById(R.id.tv_sy1);
        MyHScrollView scrollview = headView.findViewById(R.id.horizontal_scrollview);
        scrollview.setFragemnt(mFragment);
        scrollview.setActivity(mActivity);
        LinearLayout llScroll = headView.findViewById(R.id.ll_scroll);
        tvSy0.setText(mTotals[0]);
        tvSy1.setText(mTotals[1]);
        LayoutParams lp = (LayoutParams) llScroll.getLayoutParams();
        lp.width = screenWidth / 4 * mOthers.length;
        llScroll.setLayoutParams(lp);
        mHScrollViews.add(scrollview);

        for (int i = 0; i < mOthers.length; i++){
            View item = inflater.inflate(R.layout.tab_head_detail, null);
            TextView tvItem0 = item.findViewById(R.id.tv_item0);
            LinearLayout.LayoutParams tvlp = (LinearLayout.LayoutParams) tvItem0.getLayoutParams();
            tvlp.width = screenWidth / 4;
            tvItem0.setLayoutParams(tvlp);
            tvItem0.setText(mOthers[i]);

            TextView tvItem1 = item.findViewById(R.id.tv_item1);
            LinearLayout.LayoutParams tvlp1 = (LinearLayout.LayoutParams) tvItem1.getLayoutParams();
            tvlp1.width = screenWidth / 4;
            tvItem1.setLayoutParams(tvlp1);
            String s = mTotals[i + 2];
            tvItem1.setText(s);
            if (!TextUtils.isEmpty(s)){
               tvItem1.setTextColor(s.startsWith("-") ? ContextCompat.getColor(context,R.color.green) : ContextCompat.getColor(context,R.color.red));
            }
            llScroll.addView(item);
        }


        headView.setTag(StickyScrollView.STICKY);
        addView(headView);
    }

    private void initTabContent(){
        ConflictListView listView = new ConflictListView(context);
        mAdapter.setOnTableData(this);
        listView.setDivider(new ColorDrawable(ContextCompat.getColor(context,R.color.bg_gray)));
        listView.setDividerHeight(1);
        listView.setAdapter(mAdapter);
        addView(listView);
    }

    @Override
    public void addScrollviews(MyHScrollView scrollview) {
        if (!mHScrollViews.isEmpty()) {
            int size = mHScrollViews.size();
            MyHScrollView scrollView = mHScrollViews.get(size - 1);
            final int scrollX = scrollView.getScrollX();
            //第一次满屏后，向下滑动，有一条数据在开始时未加入
            if (scrollX != 0) {
                headView.post(()->{
                    scrollView.scrollTo(scrollX, 0);
                });
            }
        }
        mHScrollViews.add(scrollview);
    }

    public List<MyHScrollView> getScrollViews() {
        return mHScrollViews;
    }


    public DetailTableView setTotals(String[] totals){
        this.mTotals = totals;
        return this;
    }

    public DetailTableView setOthers(String[] others){
        this.mOthers = others;
        return this;
    }

    public DetailTableView setFragment(Fragment fragment){
        this.mFragment = fragment;
        return this;
    }

    public DetailTableView setActivity(Activity activity){
        this.mActivity = activity;
        return this;
    }

    public DetailTableView setAdapter(StocksDetailSyAdapter adapter){
        this.mAdapter = adapter;
        return this;
    }

    public void build(){
        init();
    }




}
